// views/index/school/index.js
const util = require('../../../utils/util')
var BASE_URL = getApp().globalData.BASE_URL

var Base64 = {
  _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  encode: function (e) {
    var t = "";
    var n, r, i, s, o, u, a;
    var f = 0;
    e = Base64._utf8_encode(e);
    while (f < e.length) {
      n = e.charCodeAt(f++);
      r = e.charCodeAt(f++);
      i = e.charCodeAt(f++);
      s = n >> 2;
      o = (n & 3) << 4 | r >> 4;
      u = (r & 15) << 2 | i >> 6;
      a = i & 63;
      if (isNaN(r)) {
        u = a = 64
      } else if (isNaN(i)) {
        a = 64
      }
      t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
    }
    return t
  },
  decode: function (e) {
    var t = "";
    var n, r, i;
    var s, o, u, a;
    var f = 0;
    e = e.replace(/[^A-Za-z0-9+/=]/g, "");
    while (f < e.length) {
      s = this._keyStr.indexOf(e.charAt(f++));
      o = this._keyStr.indexOf(e.charAt(f++));
      u = this._keyStr.indexOf(e.charAt(f++));
      a = this._keyStr.indexOf(e.charAt(f++));
      n = s << 2 | o >> 4;
      r = (o & 15) << 4 | u >> 2;
      i = (u & 3) << 6 | a;
      t = t + String.fromCharCode(n);
      if (u != 64) {
        t = t + String.fromCharCode(r)
      }
      if (a != 64) {
        t = t + String.fromCharCode(i)
      }
    }
    t = Base64._utf8_decode(t);
    return t
  },
  _utf8_encode: function (e) {
    e = e.replace(/rn/g, "n");
    var t = "";
    for (var n = 0; n < e.length; n++) {
      var r = e.charCodeAt(n);
      if (r < 128) {
        t += String.fromCharCode(r)
      } else if (r > 127 && r < 2048) {
        t += String.fromCharCode(r >> 6 | 192);
        t += String.fromCharCode(r & 63 | 128)
      } else {
        t += String.fromCharCode(r >> 12 | 224);
        t += String.fromCharCode(r >> 6 & 63 | 128);
        t += String.fromCharCode(r & 63 | 128)
      }
    }
    return t
  },
  _utf8_decode: function (e) {
    var t = "";
    var n = 0;
    var r = c1 = c2 = 0;
    while (n < e.length) {
      r = e.charCodeAt(n);
      if (r < 128) {
        t += String.fromCharCode(r);
        n++
      } else if (r > 191 && r < 224) {
        c2 = e.charCodeAt(n + 1);
        t += String.fromCharCode((r & 31) << 6 | c2 & 63);
        n += 2
      } else {
        c2 = e.charCodeAt(n + 1);
        c3 = e.charCodeAt(n + 2);
        t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
        n += 3
      }
    }
    return t
  }
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ke: '所有', //毕业学位
    yu: '所有', //授课语言
    value: '',
    active: 0,
    _num: 0, //院校推荐active类名
    schoollist: '', //学校列表
    showloading: true,
    page: 1,
    showfinish: false,
    total: '',
    majorlist: '', //专业列表
    card: '',
    current_page: '', //当前页
    last_page: "", //最后页,
    yuyanschool: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */

  //首先进入渲染院校推荐
  onLoad: function (options) {
    var _this = this;
    wx.request({
      url: `${BASE_URL}/school/lists.html`,
      method: "POST",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        res.data.data.forEach(function (item, index) {
          item.content = "",
          item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data.data[index].img.split(",")[0]
        })
        _this.setData({
          card: '院校推荐',
          schoollist: res.data.data,
          showloading: false,
          total: res.data.total
        })
      }
    })
    wx.request({
      url: `${BASE_URL}/study/lists/city/全国站.html`,
      method: "POST",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        res.data.data.forEach(function (item, index) {
          item.loca_photo = ""
        })
        _this.setData({
          yuyanschool: res.data.data
        })
      }
    })
  },
 
  //搜索绑定函数
  onChange: function (e) {
    this.setData({
      value: e.detail,
    });
    console.log(this.data.value)
  },
  //tabs切换绑定函数
  Change(event) {
    if (event.detail.title === '热门专业') {
      this.getallMajorlist()
    }
    this.setData({
      showloading: true,
      card: event.detail.title
    })
  },
  //获取全部专业列表
  getallMajorlist: function () {
    var _this = this;
    wx.request({
      url: `${BASE_URL}/school/major.html`,
      method: "POST",
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res)
        res.data.data.forEach(function (item, index) {
          item.dianji = item.dianji / 138 * 5,
          item.logo=`${BASE_URL}/uploads/school/`+item.logo
        })
        _this.setData({
          majorlist: res.data.data,
          current_page: res.data.current_page,
          last_page: res.data.last_page
        })
      }
    })
  },
  goranking: function () {
    wx.navigateTo({
      url: `./ranking/index`
    })
  },

  //院校推荐切换处理函数
  handleNav: function (e) {
    this.setData({
      _num: e.target.dataset.num
    })
    if (e.target.dataset.num === 0) {
      var _this = this;
      wx.request({
        url: `${BASE_URL}/school/lists.html`,
        method: "POST",
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          res.data.data.forEach(function (item, index) {
            item.content = "",
            item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data.data[index].img.split(",")[0]
          })
          _this.setData({
            schoollist: res.data.data,
            showloading: false,
            total: res.data.total,
            page: 1
          })
        }
      })
    } else {
      var _this = this;
      wx.request({
        url: `${BASE_URL}/school/lists/id/${e.target.dataset.num}.html`,
        method: "POST",
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          res.data.data.forEach(function (item, index) {
            item.content = "",
            item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data.data[index].img.split(",")[0]
          })
          _this.setData({
            schoollist: res.data.data,
            showloading: false,
            total: res.data.total,
            page: 1
          })
        }
      })
    }
  },


  //热门专业切换处理函数
  handleMajor: function (e) {
    var _this = this
    if (e.target.dataset.cate) {
      this.setData({
        ke: e.target.dataset.cate,
      })
      if (_this.data.ke !== '所有' && _this.data.yu == '所有') {
        wx.request({
          url: `${BASE_URL}/school/major/ke/${Base64.encode(_this.data.ke).replace(/\+/g,'%252B')}.html`,
          header: {
            'Content-Type': 'application/json', //GET方式是这个
          },
          method: "POST",
          success(res) {
            res.data.data.forEach(function (item, index) {
              item.dianji = item.dianji / 138 * 5,
              item.logo=`${BASE_URL}/uploads/school/`+item.logo

            })
            _this.setData({
              majorlist: res.data.data,
              current_page: res.data.current_page,
              last_page: res.data.last_page
            })
          }
        })
      }
      if (e.target.dataset.cate == "所有" && _this.data.yu !== "所有") {
        wx.request({
          url: `${BASE_URL}/school/major/yu/${Base64.encode(_this.data.yu).replace(/\+/g,'%252B')}.html`,
          header: {
            'Content-Type': 'application/json', //GET方式是这个
          },
          method: "POST",
          success(res) {
            res.data.data.forEach(function (item, index) {
              item.dianji = item.dianji / 138 * 5,
              item.logo=`${BASE_URL}/uploads/school/`+item.logo
            })
            _this.setData({
              majorlist: res.data.data,
              current_page: res.data.current_page,
              last_page: res.data.last_page
            })
          }
        })
      }
    }
    if (e.target.dataset.yuyan) {
      this.setData({
        yu: e.target.dataset.yuyan,
      })
      if (_this.data.yu !== '所有' && _this.data.ke == '所有') {
        wx.request({
          url: `${BASE_URL}/school/major/yu/${Base64.encode(_this.data.yu).replace(/\+/g,'%252B')}.html`,
          header: {
            'Content-Type': 'application/json', //GET方式是这个
          },
          method: "POST",
          success(res) {
            res.data.data.forEach(function (item, index) {
              item.dianji = item.dianji / 138 * 5,
              item.logo=`${BASE_URL}/uploads/school/`+item.logo
            })
            _this.setData({
              majorlist: res.data.data,
              current_page: res.data.current_page,
              last_page: res.data.last_page
            })
          }
        })
      }
      if (e.target.dataset.yuyan == "所有" && _this.data.ke !== "所有") {
        wx.request({
          url: `${BASE_URL}/school/major/ke/${Base64.encode(_this.data.ke).replace(/\+/g,'%252B')}.html`,
          header: {
            'Content-Type': 'application/json', //GET方式是这个
          },
          method: "POST",
          success(res) {
            res.data.data.forEach(function (item, index) {
              item.dianji = item.dianji / 138 * 5,
              item.logo=`${BASE_URL}/uploads/school/`+item.logo
            })
            _this.setData({
              majorlist: res.data.data,
              current_page: res.data.current_page,
              last_page: res.data.last_page
            })
          }
        })
      }
    }
    if (_this.data.yu !== '所有' && _this.data.ke !== '所有') {
      wx.request({
        url: `${BASE_URL}/school/major/ke/${Base64.encode(_this.data.ke).replace(/\+/g,'%252B')}/yu/${Base64.encode(_this.data.yu).replace(/\+/g,'%252B')}.html`,
        header: {
          'Content-Type': 'application/json', //GET方式是这个
        },
        method: "POST",
        success(res) {
          res.data.data.forEach(function (item, index) {
            item.dianji = item.dianji / 138 * 5,
            item.logo=`${BASE_URL}/uploads/school/`+item.logo
          })
          _this.setData({
            majorlist: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page
          })
          if (_this.data.majorlist[0] == undefined) {
            _this.setData({
              showfinish: true
            })
          } else {
            _this.setData({
              showfinish: false
            })
          }
        }
      })
    }
    if (_this.data.yu == '所有' && _this.data.ke == '所有') {
      _this.getallMajorlist()
    }
  },
  onShow: function () {

  },

  onReachBottom: util.throttle(function (e) {
    var _this = this
    if (_this.data.card === '院校推荐') {
      if (_this.data.schoollist.length !== _this.data.total) {
        _this.setData({
          page: _this.data.page + 1,
          showloading: true,
          showfinish: false,
        })
      } else {
        _this.setData({
          showloading: false,
          showfinish: true,
        })
      }
      if (_this.data._num === 0) {
        if (_this.data.showloading) {
          setTimeout(function () {
            wx.request({
              url: `${BASE_URL}/school/lists.html?page=${_this.data.page}`,
              header: {
                'Content-Type': 'application/json', //GET方式是这个
              },
              method: "POST",
              success(res) {
                res.data.data.forEach(function (item, index) {
                  item.content = "",
                  item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data.data[index].img.split(",")[0]
                })
                _this.setData({
                  showloading: false,
                  schoollist: _this.data.schoollist.concat(res.data.data),
                })
              }
            })
          }, 1000)
        }
      } else {
        if (_this.data.showloading) {
          setTimeout(function () {
            wx.request({
              url: `${BASE_URL}/school/lists/id/${_this.data._num}.html?page=${_this.data.page}`,
              header: {
                'Content-Type': 'application/json', //GET方式是这个
              },
              method: "POST",
              success(res) {
                res.data.data.forEach(function (item, index) {
                  item.content = "",
                  item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data.data[index].img.split(",")[0]
                })
                _this.setData({
                  showloading: false,
                  schoollist: _this.data.schoollist.concat(res.data.data),
                })
              }
            })
          }, 1000)
        }
      }
      console.log("触底了")
    } else {
      if (_this.data.current_page !== _this.data.last_page) {
        _this.setData({
          current_page: _this.data.current_page + 1,
          showloading: true,
          showfinish: false,
        })
      } else {
        _this.setData({
          showloading: false,
          showfinish: true,
        })
      }
      if (_this.data.yu == '所有' && _this.data.ke == '所有') {
        if (_this.data.showloading) {
          setTimeout(function () {
            wx.request({
              url: `${BASE_URL}/school/major.html?page=${_this.data.current_page}`,
              header: {
                'Content-Type': 'application/json', //GET方式是这个
              },
              method: "POST",
              success(res) {
                res.data.data.forEach(function (item, index) {
                  item.dianji = item.dianji / 138 * 5,
                  item.logo=`${BASE_URL}/uploads/school/`+item.logo
                })
                _this.setData({
                  showloading: false,
                  majorlist: _this.data.majorlist.concat(res.data.data),
                })
              }
            })
          }, 1000)
        }
      }
      if (_this.data.yu !== '所有' && _this.data.ke == '所有') {
        if (_this.data.showloading) {
          setTimeout(function () {
            wx.request({
              url: `${BASE_URL}/school/major/yu/${Base64.encode(_this.data.yu).replace(/\+/g,'%252B')}.html?page=${_this.data.current_page}`,
              header: {
                'Content-Type': 'application/json', //GET方式是这个
              },
              method: "POST",
              success(res) {
                res.data.data.forEach(function (item, index) {
                  item.dianji = item.dianji / 138 * 5,
                  item.logo=`${BASE_URL}/uploads/school/`+item.logo
                })
                _this.setData({
                  showloading: false,
                  majorlist: _this.data.majorlist.concat(res.data.data),
                })
              }
            })
          }, 1000)
        }
      }
      if (_this.data.yu == '所有' && _this.data.ke !== '所有') {
        if (_this.data.showloading) {
          setTimeout(function () {
            wx.request({
              url: `${BASE_URL}/school/major/ke/${Base64.encode(_this.data.ke).replace(/\+/g,'%252B')}.html?page=${_this.data.current_page}`,
              header: {
                'Content-Type': 'application/json', //GET方式是这个
              },
              method: "POST",
              success(res) {
                res.data.data.forEach(function (item, index) {
                  item.dianji = item.dianji / 138 * 5,
                  item.logo=`${BASE_URL}/uploads/school/`+item.logo
                })
                _this.setData({
                  showloading: false,
                  majorlist: _this.data.majorlist.concat(res.data.data),
                })
              }
            })
          }, 1000)
        }
      }
      if (_this.data.yu !== '所有' && _this.data.ke !== '所有') {
        if (_this.data.showloading) {
          setTimeout(function () {
            wx.request({
              url: `${BASE_URL}/school/major/ke/${Base64.encode(_this.data.ke).replace(/\+/g,'%252B')}/yu/${Base64.encode(_this.data.yu).replace(/\+/g,'%252B')}.html?page=${_this.data.current_page}`,
              header: {
                'Content-Type': 'application/json', //GET方式是这个
              },
              method: "POST",
              success(res) {
                res.data.data.forEach(function (item, index) {
                  item.dianji = item.dianji / 138 * 5,
                  item.logo=`${BASE_URL}/uploads/school/`+item.logo
                })
                _this.setData({
                  showloading: false,
                  majorlist: _this.data.majorlist.concat(res.data.data),
                })
              }
            })
          }, 1000)
        }
      }
    }
  }, 2000),


  //跳转学校详情页
  schoolDetail: function (e) {
    var id = e.currentTarget.dataset.id 
    wx.navigateTo({
      url: `/views/index/school/schooldetail/index?id=${id}`
    })
  },
   //跳转学校详情页
   majorjumpDetail: function (e) {
    var id = e.currentTarget.dataset.id 
    wx.navigateTo({
      url: `/views/index/school/schooldetail/index?id=${id}&xueliid=${e.currentTarget.dataset.xueliid}&majorid=${e.currentTarget.dataset.majorid}`
    })
  },
  //跳转更多学校
  gomoreschool: function (e) {
    var active = e.currentTarget.dataset.active 
    wx.navigateTo({
      url: `/views/index/Germanstudy/index?active=${active}`
    })
  },
  //跳转语言学校详情页
  goschooldetail:function(e){
    var id = e.currentTarget.dataset.id 
    wx.navigateTo({
      url: `/views/index/Germanstudy/schooldetail/index?id=${id}`
    })
  },
    //返回顶部
goTop: function () {
  wx.pageScrollTo({
    scrollTop: 0,
    duration: 300
  })
},
})